package com.cskaoyan.wordmemorize.controller.app;

import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.UserVocAISituationDTO;
import com.cskaoyan.wordmemorize.dto.app.*;
import com.cskaoyan.wordmemorize.request.AIReviewReadCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserFavoriteCommand;
import com.cskaoyan.wordmemorize.request.UserVocExampleSentenceCommand;
import com.cskaoyan.wordmemorize.service.UserPrivilegeService;
import com.cskaoyan.wordmemorize.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * APP用户特权相关接口控制器
 */
@RestController
@RequestMapping("/user/priviledge")
@Tag(name = "APP用户特权相关接口")
public class AppUserPrivilegeController {

    @Resource
    private UserPrivilegeService userPrivilegeService;

    /**
     * 获取当前用户词书进度
     *
     * @param bookId 词书ID
     * @return 词书进度统计信息
     */
    @GetMapping("/current/{bookId}/statistics")
    @Operation(summary = "获取当前用户词书进度")
    public Result<UserVocBookStatisticsDTO> getCurrentBookStatistics(@PathVariable("bookId") Long bookId) {
        // 获取当前登录用户的ID
        Long userId = StpKit.USER.getLoginIdAsLong();
        UserVocBookStatisticsDTO userVocBookStatistics = userPrivilegeService.getSelectedBookStatistics(userId, bookId);
        return Result.ok(userVocBookStatistics);
    }

    /**
     * 标识用户不想背某个单词
     *
     * @param vocId 单词ID
     * @return 操作结果
     */
    @PostMapping("/voc/non")
    @Operation(summary = "标识用户不想背")
    public Result<Void> notRemember(@RequestParam Long vocId) {
        // 获取当前登录用户的ID
        Long userId = StpKit.USER.getLoginIdAsLong();

        userPrivilegeService.notRemember(vocId, userId);
        return Result.ok();
    }

    /**
     * 取消用户不想背的标记
     *
     * @param vocId 单词ID
     * @return 操作结果
     */
    @PutMapping("/voc/non/cancel")
    @Operation(summary = "取消用户不想背")
    public Result<Void> cancelNotRemember(@RequestParam Long vocId) {
        // 获取当前登录用户的ID
        Long userId = StpKit.USER.getLoginIdAsLong();

        userPrivilegeService.cancelNotRemember(vocId, userId);
        return Result.ok();
    }

    /**
     * 获取用户自建例句列表
     *
     * @param pageRequest 分页请求参数
     * @return 分页的自建例句列表
     */
    @GetMapping("/voc/example/list")
    @Operation(summary = "获取用户自建例句")
    public Result<PageDTO<DisplayUserVocSentenceDTO>> getUserVocSentence(PageRequest pageRequest) {
        // 获取当前登录用户的ID
        Long userId = StpKit.USER.getLoginIdAsLong();

        PageDTO<DisplayUserVocSentenceDTO> pageDTO = userPrivilegeService.getUserVocSentence(pageRequest, userId);

        return Result.ok(pageDTO);
    }

    /**
     * 添加自建例句
     *
     * @param command 自建例句命令对象
     * @return 操作结果
     */
    @PostMapping("/voc/example")
    @Operation(summary = "添加自建例句")
    public Result<Void> addUserVocSentence(@RequestBody UserVocExampleSentenceCommand command) {
        // 获取当前登录用户的ID
        // 不要使用command里的ID，前端并未发送
        Long userId = StpKit.USER.getLoginIdAsLong();

        userPrivilegeService.addUserVocSentence(command, userId);
        return Result.ok();
    }

    /**
     * 修改用户自建例句
     *
     * @param command 自建例句命令对象
     * @return 操作结果
     */
    @PutMapping("/voc/example")
    @Operation(summary = "修改（更新）用户自建例句")
    public Result<Void> updateUserVocSentence(@RequestBody UserVocExampleSentenceCommand command) {
        // 获取当前登录用户的ID
        // 保险起见，都使用从后台获取到的ID
        Long userId = StpKit.USER.getLoginIdAsLong();

        userPrivilegeService.updateUserVocSentence(command, userId);
        return Result.ok();
    }

    /**
     * 删除用户自建例句
     *
     * @param id 例句ID
     * @return 操作结果
     */
    @DeleteMapping("/voc/example/{id}")
    @Operation(summary = "删除用户自建例句")
    public Result<Void> deleteUserVocSentence(@PathVariable Long id) {
        userPrivilegeService.deleteUserVocSentence(id);
        return Result.ok();
    }

    /**
     * 获取用户收藏单词列表
     *
     * @param pageRequest 分页请求参数
     * @return 分页的收藏单词列表
     */
    @GetMapping("/favorite/list")
    @Operation(summary = "获取用户收藏单词列表")
    public Result<PageDTO<UserFavoriteDTO>> getFavoriteList(PageRequest pageRequest) {
        // 获取当前登录用户的ID
        // 保险起见，都使用从后台获取到的ID
        Long userId = StpKit.USER.getLoginIdAsLong();

        PageDTO<UserFavoriteDTO> pageDTO = userPrivilegeService.getUserFavoriteList(pageRequest, userId);

        return Result.ok(pageDTO);

    }


    /**
     * 添加收藏单词
     *
     * @param userFavoriteCommand 收藏命令对象
     * @return 操作结果
     */
    @PostMapping("/favorite")
    @Operation(summary = "添加收藏单词")
    public Result<Void> addFavorite(@RequestBody UserFavoriteCommand userFavoriteCommand) {
        // 获取当前登录用户的ID
        // 保险起见，都使用从后台获取到的ID
        long userId = StpKit.USER.getLoginIdAsLong();

        userPrivilegeService.addFavorite(userFavoriteCommand, userId);
        return Result.ok();
    }


    /**
     * 批量添加收藏单词
     *
     * @param userFavoriteCommands 收藏命令对象列表
     * @return 操作结果
     */
    @PostMapping("/favorite/bulk")
    @Operation(summary = "批量添加收藏单词")
    public Result<Void> addFavoriteBulk(@RequestBody List<UserFavoriteCommand> userFavoriteCommands) {
//        if (userFavoriteCommands == null || userFavoriteCommands.isEmpty()) {
//            throw new IllegalArgumentException("收藏命令对象列表不能为空");
//        }
        // 获取当前登录用户的ID
        // 保险起见，都使用从后台获取到的ID
        long userId = StpKit.USER.getLoginIdAsLong();

        userPrivilegeService.addFavoriteBulk(userFavoriteCommands, userId);
        return Result.ok();
    }

    /**
     * 取消收藏单词
     *
     * @param ids 单词ID列表
     * @return 操作结果
     */
    @PostMapping("/favorite/cancel")
    @Operation(summary = "取消收藏单词")
    public Result<Void> cancelFavorite(@RequestBody List<Long> ids) {
        // 获取当前登录用户的ID
        Long userId = StpKit.USER.getLoginIdAsLong();

        userPrivilegeService.cancelFavorite(ids, userId);
        return Result.ok();
    }

    /**
     * 获取AI阅读内容
     *
     * @param command AI阅读请求体
     * @return AI阅读内容列表
     */
    @PostMapping("/review/ai/read")
    @Operation(summary = "AI阅读")
    public Result<List<AIReviewReadDTO>> getAIReviewReadContent(@RequestBody AIReviewReadCommand command) {
        // 获取当前登录用户的ID
        Long userId = StpKit.USER.getLoginIdAsLong();

        List<AIReviewReadDTO> aiReviewReadDTOS = userPrivilegeService.getAIReviewReadContent(command, userId);

        return Result.ok(aiReviewReadDTOS);
    }


    /**
     * AI阅读风格选项
     *
     * @return AI阅读风格选项列表
     */
    @PostMapping("/review/ai/read/options")
    @Operation(summary = "AI阅读风格选项")
    public Result<List<UserVocAISituationDTO>> getAIReviewReadOptions() {

        List<UserVocAISituationDTO> userVocAISituationDTOS = userPrivilegeService.getAIReviewReadOptions();

        return Result.ok(userVocAISituationDTOS);
    }


    /**
     * 获取用户AI阅读剩余免费使用次数
     *
     * @return 剩余免费次数
     */
    @PostMapping("/review/ai/read/free")
    @Operation(summary = "AI阅读剩余免费使用次数")
    public Result<Integer> getUserVocAIReadRemainFreeCount() {
        // 获取当前登录用户的ID
        Long userId = StpKit.USER.getLoginIdAsLong();
        int remainCount = userPrivilegeService.getUserVocAIReadRemainFreeCount(userId);
        return Result.ok(remainCount);
    }
}
